package com.ruoyi.project.modules.product.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.project.modules.business.domain.BusinessInfo;
import com.ruoyi.project.modules.business.service.IBusinessInfoService;
import com.ruoyi.project.modules.businessSystem.domain.BusinessSystem;
import com.ruoyi.project.modules.businessSystem.service.IBusinessSystemService;
import com.ruoyi.project.modules.businessUsage.domain.BusinessUsage;
import com.ruoyi.project.modules.businessUsage.service.IBusinessUsageService;
import com.ruoyi.project.modules.category.domain.Category;
import com.ruoyi.project.modules.interfaceObject.domain.InterfaceObject;
import com.ruoyi.project.modules.interfaceObject.service.IInterfaceObjectService;
import com.ruoyi.project.modules.interfaceinfo.domain.InterfaceInfo;
import com.ruoyi.project.modules.interfaceinfo.domain.VirtualUsage;
import com.ruoyi.project.modules.interfaceinfo.service.IInterfaceInfoService;
import com.ruoyi.project.modules.interfaceinfo.service.IVirtualUsageService;
import com.ruoyi.project.modules.page.domain.Page;
import com.ruoyi.project.modules.page.service.IPageService;
import com.ruoyi.project.modules.productVersionObejct.domain.ProductVersionObejct;
import com.ruoyi.project.modules.productVersionObejct.service.IProductVersionObejctService;
import com.ruoyi.project.modules.table.domain.TableInfo;
import com.ruoyi.project.modules.table.service.ITableInfoService;
import com.ruoyi.project.modules.tableObject.domain.TableObject;
import com.ruoyi.project.modules.tableObject.service.ITableObjectService;
import com.ruoyi.project.modules.usage.domain.Usage;
import com.ruoyi.project.modules.usage.service.IUsageService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.modules.product.domain.ProductInfo;
import com.ruoyi.project.modules.product.service.IProductInfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 产品信息Controller
 *
 * @author smallrain
 * @date 2021-10-28
 */
@Controller
@RequestMapping("/product/productInfo")
public class ProductInfoController extends BaseController {
    private String prefix = "product/productInfo";

    @Autowired
    private IProductInfoService productInfoService;

    @RequiresPermissions("product:productInfo:view")
    @GetMapping()
    public String productInfo() {
        return prefix + "/productInfo";
    }

    /**
     * 查询产品信息列表
     */
    @RequiresPermissions("product:productInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProductInfo productInfo) {
        startPage();
        productInfo.setUserId(ShiroUtils.getSysUser().getUserId());
        productInfo.setFlag(this.getFlag());
        List<ProductInfo> list = productInfoService.selectProductInfoList(productInfo);
        return getDataTable(list);
    }

    /**
     * 根据不同类型，查询版本信息列表
     * @param versionType 1-接口，2-页面
     * @return
     */
    @GetMapping("/versionList")
    @ResponseBody
    public AjaxResult versionList(String versionType) {
        ProductInfo productInfo = new ProductInfo();
        productInfo.setVersionType(versionType);
        List<ProductInfo> list = productInfoService.selectProductInfoList(productInfo);
        return AjaxResult.success(list);
    }

    /**
     * 导出产品信息列表
     */
    @RequiresPermissions("product:productInfo:export")
    @Log(title = "产品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProductInfo productInfo) {
        List<ProductInfo> list = productInfoService.selectProductInfoList(productInfo);
        ExcelUtil<ProductInfo> util = new ExcelUtil<ProductInfo>(ProductInfo.class);
        return util.exportExcel(list, "产品信息数据");
    }

    @Autowired
    private IInterfaceInfoService interfaceInfoService;
    @Autowired
    private IProductVersionObejctService productVersionObejctService;

    /**
     * 新增产品信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
//        InterfaceInfo interfaceInfo = new InterfaceInfo();
//        interfaceInfo.setFbType("2");
//        List<InterfaceInfo> interfaceInfoList = this.interfaceInfoService.selectInterfaceInfoList(interfaceInfo);
//        mmap.put("interfaceInfoList", interfaceInfoList);
        return prefix + "/add";
    }

    /**
     * 新增保存产品信息
     */
    @RequiresPermissions("product:productInfo:add")
    @Log(title = "产品信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProductInfo productInfo) {
        return toAjax(productInfoService.insertProductInfo(productInfo));
    }

    /**
     * 修改产品信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        ProductInfo productInfo = productInfoService.selectProductInfoById(id);
        mmap.put("productInfo", productInfo);
//        InterfaceInfo interfaceInfo = new InterfaceInfo();
//        interfaceInfo.setFbType("2");
//        List<InterfaceInfo> interfaceInfoList = this.interfaceInfoService.selectInterfaceInfoList(interfaceInfo);
//        mmap.put("interfaceInfoList", interfaceInfoList);
//        ProductVersionObejct productVersionObejct = new ProductVersionObejct();
//        productVersionObejct.setTableProductVersionId(id);
//        productVersionObejct.setObjectType("5");
//        List<ProductVersionObejct> productVersionObejcts = productVersionObejctService.selectProductVersionObejctList(productVersionObejct);
//        if (CollectionUtil.isNotEmpty(productVersionObejcts)) {
//            List<Long> interfaceIds = new ArrayList<>();
//            productVersionObejcts.forEach(po -> interfaceIds.add(po.getObjectId()));
//            mmap.put("interfaceIds", interfaceIds);
//        }

        return prefix + "/edit";
    }

    /**
     * 修改保存产品信息
     */
    @RequiresPermissions("product:productInfo:edit")
    @Log(title = "产品信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProductInfo productInfo) {
//        ProductInfo productInfo1 = this.productInfoService.selectProductInfoById(productInfo.getId());
//        if (!productInfo.getVersionType().equals(productInfo1.getVersionType())) {
//            ProductVersionObejct productVersionObejct = new ProductVersionObejct();
//            productVersionObejct.setTableProductVersionId(productInfo.getId());
//            if ("1".equals(productInfo1.getVersionType())) {
//                productVersionObejct.setObjectType("5");
//            } else {
//                productVersionObejct.setObjectType("2");
//            }
//
//            List<ProductVersionObejct> productVersionObejcts = productVersionObejctService.selectProductVersionObejctList(productVersionObejct);
//            if (CollectionUtil.isNotEmpty(productVersionObejcts)) {
//                return AjaxResult.warn("产品版本下有"+("1".equals(productInfo1.getVersionType())?"接口":"页面"));
//            }
//        }
        productInfo.setVersionType(null);
        return toAjax(productInfoService.updateProductInfo(productInfo));
    }

    /**
     * 删除产品信息
     */
    @RequiresPermissions("product:productInfo:remove")
    @Log(title = "产品信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
//        ProductInfo productInfo = this.productInfoService.selectProductInfoById(Long.parseLong(ids));
//        ProductVersionObejct productVersionObejct = new ProductVersionObejct();
//        productVersionObejct.setTableProductVersionId(productInfo.getId());
//        if ("1".equals(productInfo.getVersionType())) {
//            productVersionObejct.setObjectType("5");
//        } else {
//            productVersionObejct.setObjectType("2");
//        }
//
//        List<ProductVersionObejct> productVersionObejcts = productVersionObejctService.selectProductVersionObejctList(productVersionObejct);
//        if (CollectionUtil.isNotEmpty(productVersionObejcts)) {
//            return AjaxResult.warn("产品版本下有"+("1".equals(productInfo.getVersionType())?"接口":"页面"));
//        }
        return toAjax(productInfoService.deleteProductInfoByIds(ids));
    }

    @Autowired
    private IUsageService usageService;

    @Autowired
    private IInterfaceObjectService interfaceObjectService;

    @GetMapping("/usageList")
    public String usageList(@RequestParam Long id, ModelMap mmap) {
        ProductInfo productInfo = this.productInfoService.selectProductInfoById(id);
        mmap.put("productInfo", productInfo);

        // 查询接口
        ProductVersionObejct productVersionObejct = new ProductVersionObejct();
        productVersionObejct.setTableProductVersionId(id);
        productVersionObejct.setObjectType("5");
        List<ProductVersionObejct> productVersionObejcts = productVersionObejctService.selectProductVersionObejctList(productVersionObejct);
        if (CollectionUtil.isNotEmpty(productVersionObejcts)) {
            Set<Long> interfaceIds = new HashSet<>();
            productVersionObejcts.forEach(ib -> interfaceIds.add(ib.getObjectId()));
            mmap.put("interfaceIds", interfaceIds);

            // 通过接口，查询用况
            InterfaceObject interfaceObject2 = new InterfaceObject();
            interfaceObject2.setBffIds(interfaceIds);
            interfaceObject2.setObjectType("3");
            List<InterfaceObject> interfaceObjectList = interfaceObjectService.selectInterfaceObjectList(interfaceObject2);
            if (CollectionUtil.isNotEmpty(interfaceObjectList)) {
                Set<Long> usageIds = new HashSet<>();
                interfaceObjectList.forEach(ib -> usageIds.add(ib.getObjectId()));

                Usage usage = new Usage();
                usage.setUsageIds(usageIds);
                List<Usage> usageList = usageService.selectUsageList(usage);
                mmap.put("usageList", usageList);
            }
        }
        return prefix + "/usageList";
    }

    @Resource
    private IVirtualUsageService virtualUsageService;

    @Resource
    private ITableInfoService tableInfoService;

    @GetMapping("/tableList")
    public String tableList(@RequestParam Long id, ModelMap mmap) {
        ProductInfo productInfo = this.productInfoService.selectProductInfoById(id);
        mmap.put("productInfo", productInfo);

        // 查询接口
        ProductVersionObejct productVersionObejct = new ProductVersionObejct();
        productVersionObejct.setTableProductVersionId(id);
        productVersionObejct.setObjectType("5");
        List<ProductVersionObejct> productVersionObejcts = productVersionObejctService.selectProductVersionObejctList(productVersionObejct);
        if (CollectionUtil.isNotEmpty(productVersionObejcts)) {
            Set<Long> interfaceIds = new HashSet<>();
            productVersionObejcts.forEach(ib -> interfaceIds.add(ib.getObjectId()));
            mmap.put("interfaceIds", interfaceIds);

            List<TableInfo> tableList = this.businessSystemService.tableList(interfaceIds);
            mmap.put("tableList", tableList);
        }
        return prefix + "/tableList";
    }

    @Autowired
    private IPageService pageService;

    @GetMapping("/pageList")
    public String pageList(@RequestParam Long id, ModelMap mmap) {
        ProductInfo productInfo = this.productInfoService.selectProductInfoById(id);
        mmap.put("productInfo", productInfo);
        return prefix + "/pageList";
    }

    @PostMapping("/pageList")
    @ResponseBody
    public List<Page> pageList(@RequestParam Long id) {
        // 查询接口
        ProductVersionObejct productVersionObejct = new ProductVersionObejct();
        productVersionObejct.setTableProductVersionId(id);
        productVersionObejct.setObjectType("5");
        List<ProductVersionObejct> productVersionObejcts = productVersionObejctService.selectProductVersionObejctList(productVersionObejct);
        if (CollectionUtil.isNotEmpty(productVersionObejcts)) {
            Set<Long> interfaceIds = new HashSet<>();
            productVersionObejcts.forEach(ib -> interfaceIds.add(ib.getObjectId()));
            // 查页面
            InterfaceObject interfaceObject2 = new InterfaceObject();
            interfaceObject2.setBffIds(interfaceIds);
            interfaceObject2.setObjectType("2");
            List<InterfaceObject> interfaceObjectList = this.interfaceObjectService.selectInterfaceObjectList(interfaceObject2);
            if (CollectionUtil.isNotEmpty(interfaceObjectList)) {
                Set<Long> pageIds = interfaceObjectList.stream().map(a -> a.getObjectId()).collect(Collectors.toSet());
                Page page = new Page();
                page.setPageIds(pageIds);
                List<Page> pageList = this.pageService.selectPageList(page);
                return pageList;
            }
        }

        return new ArrayList<>();
    }

    @Autowired
    private IBusinessUsageService businessUsageService;
    @Autowired
    private IBusinessInfoService businessInfoService;

    @GetMapping("/businessInfoList")
    public String businessInfoList(@RequestParam Long id, ModelMap mmap) {
        ProductInfo productInfo = this.productInfoService.selectProductInfoById(id);
        mmap.put("productInfo", productInfo);

        // 查询接口
        ProductVersionObejct productVersionObejct = new ProductVersionObejct();
        productVersionObejct.setTableProductVersionId(id);
        productVersionObejct.setObjectType("5");
        List<ProductVersionObejct> productVersionObejcts = productVersionObejctService.selectProductVersionObejctList(productVersionObejct);
        if (CollectionUtil.isNotEmpty(productVersionObejcts)) {
            Set<Long> interfaceIds = new HashSet<>();
            productVersionObejcts.forEach(ib -> interfaceIds.add(ib.getObjectId()));
            // 通过接口，查询用况
            InterfaceObject interfaceObject2 = new InterfaceObject();
            interfaceObject2.setBffIds(interfaceIds);
            interfaceObject2.setObjectType("3");
            List<InterfaceObject> interfaceObjectList = interfaceObjectService.selectInterfaceObjectList(interfaceObject2);
            if (CollectionUtil.isNotEmpty(interfaceObjectList)) {
                Set<Long> usageIds = new HashSet<>();
                interfaceObjectList.forEach(ib -> usageIds.add(ib.getObjectId()));
                // 通过用况，查询业务需求
                BusinessUsage businessUsage = new BusinessUsage();
                businessUsage.setUsageIds(usageIds);
                List<BusinessUsage> businessUsageList = this.businessUsageService.selectBusinessUsageList(businessUsage);

                if (CollectionUtil.isNotEmpty(businessUsageList)) {
                    Set<Long> businessInfoIds = businessUsageList.stream().map(a -> a.getBusinessId()).collect(Collectors.toSet());
                    BusinessInfo businessInfo = new BusinessInfo();
                    businessInfo.setBusinessInfoIds(businessInfoIds);

                    List<BusinessInfo> businessInfoList = this.businessInfoService.selectBusinessInfoList(businessInfo);
                    mmap.put("businessInfoList", businessInfoList);
                }
            }
        }

        return prefix + "/businessInfoList";
    }

    @Autowired
    private ITableObjectService tableObjectService;
    @Autowired
    private IBusinessSystemService businessSystemService;

    @GetMapping("/businessSystemList")
    public String businessSystemList(@RequestParam Long id, ModelMap mmap) {
        ProductInfo productInfo = this.productInfoService.selectProductInfoById(id);
        mmap.put("productInfo", productInfo);

        // 查询接口
        ProductVersionObejct productVersionObejct = new ProductVersionObejct();
        productVersionObejct.setTableProductVersionId(id);
        productVersionObejct.setObjectType("5");
        List<ProductVersionObejct> productVersionObejcts = productVersionObejctService.selectProductVersionObejctList(productVersionObejct);
        if (CollectionUtil.isNotEmpty(productVersionObejcts)) {
            Set<Long> interfaceIds = new HashSet<>();
            productVersionObejcts.forEach(ib -> interfaceIds.add(ib.getObjectId()));

            List<TableInfo> tableList = this.businessSystemService.tableList(interfaceIds);
            if (CollectionUtil.isNotEmpty(tableList)) {
                Set<Long> tableIds = tableList.stream().map(TableInfo::getId).collect(Collectors.toSet());
                TableObject tableObject = new TableObject();
                tableObject.setTableIds(tableIds);
                tableObject.setObjectType("1");
                List<TableObject> tableObjects = this.tableObjectService.selectTableObjectList(tableObject);
                if (CollectionUtil.isNotEmpty(tableObjects)) {
                    Set<Long> businessSystemIds = tableObjects.stream().map(TableObject::getObjectId).collect(Collectors.toSet());
                    BusinessSystem businessSystem = new BusinessSystem();
                    businessSystem.setBusinessSystemIds(businessSystemIds);
                    List<BusinessSystem> businessSystemList = this.businessSystemService.selectBusinessSystemList(businessSystem);
                    mmap.put("businessSystemList", businessSystemList);
                }
            }
        }

        return prefix + "/businessSystemList";
    }
}
